package com.moleq.base;

import android.util.Log;
import android.view.View;

import com.moleq.device.DeviceManager;
import com.moleq.enums.ItemType;
import com.moleq.logic.PosPrinter;
import com.moleq.logic.TransactionManager;
import com.moleq.models.GlobalInfo;
import com.moleq.models.SaleListItem;
import com.moleq.util.MoleQUtil;
import com.moleq.util.StringUtils;
import com.stlcctv.POSSender.CommandComposer;
import com.stlcctv.POSSender.POSSenderActivity;

public class MGposSender extends POSSenderActivity {

	
	private static MGposSender instance;
	
	public static final int AMT = 8;
	public static final int NAME = 20;
	public static final int QTY = 3;
	public static final int ALL = 31;
	
	public static final int RED = 0;
	public static final int WHITE = 1;
	
	public static MGposSender getInstance() {
		if (instance == null) {
			instance = new MGposSender();

		}
		return instance;
	}
	
	public void setIpAddress(String ip,int port){
		this.m_ipaddress = ip;
		this.m_port = port;
	}
	
	
	public void start(){
		m_Thread_Open = new Thread(new OpenThread());
		m_Thread_Open.start();
	}
	
	public void stop(){
		StopRecvThread();
		ReleaseSocket();
	}

	
	public void send(String msg){
		
		if(m_bDVRConnected == true)
		{
			CommandComposer _Composer = new CommandComposer();
			_Composer.m_CmdCode  = _Composer.CMD_CODE_POS_DATA;
			byte[] sendBuffer = new byte[100];
			if (msg.length() > 33) {
				msg = msg.substring(0, 33);
			}
			String pos_data = msg;
			
			int bufSize = _Composer.MakeCommand_POSSend(sendBuffer,pos_data,(short) pos_data.length());
			Log.i(DEBUG_TAG,"Send Data2 bufSize : " + bufSize);
			SendStreamData(sendBuffer, bufSize);
			
			sendBuffer = null;
			_Composer = null;
			pos_data = null;
		}
		
	}
	
	public void sendRed(String msg){
		
		if(m_bDVRConnected == true)
		{
			CommandComposer _Composer = new CommandComposer();
			_Composer.m_CmdCode  = _Composer.CMD_CODE_TRANSACTION_START;
			byte[] sendBuffer = new byte[100];
			if (msg.length() > 33) {
				msg = msg.substring(0, 33);
			}
			String pos_data = msg;
			
			int bufSize = _Composer.MakeCommand_POSSend(sendBuffer,pos_data,(short) pos_data.length());
			Log.i(DEBUG_TAG,"Send Data2 bufSize : " + bufSize);
			SendStreamData(sendBuffer, bufSize);
			
			sendBuffer = null;
			_Composer = null;
			pos_data = null;
		}
		
	}
	
	public void sendItemContext(SaleListItem item,int textColor){
		int iTotalLen = 33;
		int iP4Len = 14;
		int iContentLen = iTotalLen - iP4Len;
		String sLine = "";
		String sPrice = "";
		int iLen = 0;
		boolean showTax = false;
		boolean showFootstamp = false;
			sLine = "";

			SaleListItem saleitem = item;
			iLen = 0;

			String sName = saleitem.getName();
			if (saleitem.getItemType() == ItemType.PACKAGE_PRICE_FIX || saleitem.getItemType() == ItemType.LIMITED_QTY_PRICE_FIX
				||	saleitem.getItemType() == ItemType.DISCOUNT_AMOUNT || saleitem.getItemType() == ItemType.DISCOUNT_PERECENT
				||	saleitem.getItemType() == ItemType.DISCOUNT_LINK) {
				sName = item.getDisplayContent();
			}
			if (saleitem.isRefund()) {
				if (saleitem.getItemType() == ItemType.REGULAR || saleitem.getItemType() == ItemType.OPEN_DEPARTMENT) {
					sName = sName + " (Refund)";
				}
			}
			// qty
			if ((saleitem.getItemType() == ItemType.REGULAR || saleitem.getItemType() == ItemType.OPEN_DEPARTMENT) 
					&& !saleitem.getEachDescription().equals("")) {
				String sQty = String.valueOf(saleitem.getQty());
				sName = sQty + " " + sName;
			}
			iLen = sLine.length();
			if (sName.length() + iLen > iContentLen) {

				sName = sName.substring(0, iContentLen - iLen);
				sLine += sName;
			} else {
				iLen = iContentLen - (sName.length() + iLen);
				sLine += sName + StringUtils.getString(" ", iLen);
			}

			showTax = false;
			showFootstamp = false;
			if (saleitem.getTax1() || saleitem.getTax2() || saleitem.getTax3() || saleitem.getTax4() || saleitem.getTax5() || saleitem.getTax6()) {
				showTax = true;
			}

			if (saleitem.getFoodstamp()) {
				showFootstamp = true;
			}

			// price
			sPrice = "  " + MoleQUtil.fixPrintPrice(saleitem.getPrice(), showFootstamp, showTax);
			iLen = iP4Len - sPrice.length();
			if (iLen == 0) {
				sLine += sPrice;
			} else {
				sLine += StringUtils.getString(" ", iLen) + sPrice;

			}
			
			if (!item.getEachDescription().equals("")) {
				if (textColor == RED) {
					sendRed(item.getEachDescription());
				}else{
					send(item.getEachDescription());
				}
			}

			if (!item.getPriceDescription().equals("")) {
				if (textColor == RED) {
					sendRed(item.getPriceDescription() + item.getmTareDesc());
				}else{
					send(item.getPriceDescription() + item.getmTareDesc());
				}
				
			}
			//show dep weight 
			if (!item.getDepDescription().equals("")) {
				if (textColor == RED) {
					sendRed(item.getDepDescription());
				}else{
					send(item.getDepDescription());
				}
			}
			
			if (!sLine.equals("")) {
				if (textColor == RED) {
					sendRed(sLine);
				}else{
					send(sLine);
				}
			}
			
			if (GlobalInfo.getInstance().isShowDesc && !item.getDescription().equals("")) {
				
				if (textColor == RED) {
					sendRed(item.getDescription());
				}else{
					send(item.getDescription());
				}
			}
			
		
	}

	public void Prepare() {
		// TODO Auto-generated method stub
		if(m_bDVRConnected == false)
			Request_AliveCheck();
	}
}
